/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.SqlJetTransactionMode;
import org.tmatesoft.sqljet.core.table.ISqlJetCursor;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;

/**
 * Liest die Daten der Datenbank ein
 * @author mordredd666
 *
 */
public class Datenbankeinleser
{

	private static String DB_NAME = "Datenbank.sqlite";
	private static String DB_TABLE = "ZFRAGE";

	private static String ZFRAGEID = "ZFRAGEID";

	private static String ZFRAGETEXT = "ZFRAGETEXT";
	private static String ZBILD = "ZBILD";
	private static String ZANTWORT1 = "ZANTWORT1";
	private static String ZANTWORT2 = "ZANTWORT2";
	private static String ZANTWORT3 = "ZANTWORT3";
	private static String ZANTWORT4 = "ZANTWORT4";
	private static String ZANTWORT5 = "ZANTWORT5";
	private static String ZANTWORTBILD1 = "ZANTWORTBILD1";
	private static String ZANTWORTBILD2 = "ZANTWORTBILD2";
	private static String ZANTWORTBILD3 = "ZANTWORTBILD3";
	private static String ZANTWORTBILD4 = "ZANTWORTBILD4";
	private static String ZANTWORTBILD5 = "ZANTWORTBILD5";
	private static String ZRICHTIGEANTWORT = "ZRICHTIGEANTWORT";
	
	@SuppressWarnings("unused")
	private static String ZLETZTEANTWORT = "ZLETZTEANTWORT";

	private static String ZFACH = "ZFACH";
	private static String ZTHEMA = "ZTHEMA";
	private static String ZJAHR = "ZJAHR";
	private static String ZNOTIZ = "ZNOTIZ";
	private static String ZFRAGENUMMER = "ZFRAGENUMMER";
	private static String ZFRAGETAG = "ZFRAGETAG";

	private static ArrayList<Frage> _fragen;

	private static HashMap<String, ArrayList<String>> _fächerplusthemen;

	private static ArrayList<Integer> _examinaJahre;

	/**
	 * Liest bei Bedarf alle Fragen und gibt sie aus
	 * 
	 * @return {@link Collection} of {@link Frage}
	 */
	public static ArrayList<Frage> getFragen()
	{
		if (_fragen == null)
			try
			{
				leseEin();
			}
			catch (SqlJetException e)
			{
				return new ArrayList<Frage>();
			}

		return _fragen;
	}

	public static ArrayList<String> getFächer()
	{
		ArrayList<String> s = new ArrayList<String>();
		for (String string : _fächerplusthemen.keySet())
		{
			s.add(string);
		}

		Collections.sort(s);
		return s;
	}

	public static ArrayList<String> getThemenFürFach(String fach)
	{
		return _fächerplusthemen.get(fach);
	}

	public static ArrayList<Integer> getExaminaJahre()
	{
		Collections.sort(_examinaJahre);
		return _examinaJahre;
	}

	private static void leseEin() throws SqlJetException
	{
		File fileDB = new File(DB_NAME);
		
		SqlJetDb db = SqlJetDb.open(fileDB, true);

		_fragen = new ArrayList<Frage>();
		_fächerplusthemen = new HashMap<String, ArrayList<String>>();
		_examinaJahre = new ArrayList<Integer>();

		ISqlJetTable table = db.getTable(DB_TABLE);
		db.beginTransaction(SqlJetTransactionMode.READ_ONLY);

		try
		{

			readAll(table.order(table.getPrimaryKeyIndexName()));
		}
		finally
		{
			db.commit();
		}
	}

	private static void readAll(ISqlJetCursor cursor) throws SqlJetException
	{
		try
		{
			if (!cursor.eof())
			{
				do
				{

					int id = (int) cursor.getInteger(ZFRAGEID);

					String fragetext = cursor.getString(ZFRAGETEXT);
					
//					String bild = cursor.getString(ZBILD);
					byte bild[] = cursor.getBlobAsArray(ZBILD);

					String[] antworten =
					{ cursor.getString(ZANTWORT1), cursor.getString(ZANTWORT2),
							cursor.getString(ZANTWORT3), cursor.getString(ZANTWORT4),
							cursor.getString(ZANTWORT5) };
					for(String s : antworten)
					{
						if (s==null)
								s = "";
					}

					Object[] antwortbilder =
					{ cursor.getBlobAsArray(ZANTWORTBILD1), cursor.getBlobAsArray(ZANTWORTBILD2),
							cursor.getBlobAsArray(ZANTWORTBILD3), cursor.getBlobAsArray(ZANTWORTBILD4),
							cursor.getBlobAsArray(ZANTWORTBILD5) };

					int richtigeantwort = (int) cursor.getInteger(ZRICHTIGEANTWORT);

					String fach = cursor.getString(ZFACH);
					String thema = cursor.getString(ZTHEMA);

					if (!_fächerplusthemen.containsKey(fach))
					{
						ArrayList<String> s = new ArrayList<String>();
						s.add(thema);
						_fächerplusthemen.put(fach, s);
					}

					if (!_fächerplusthemen.get(fach).contains(thema))
					{
						_fächerplusthemen.get(fach).add(thema);
					}

					int jahr = (int) cursor.getInteger(ZJAHR);

					if (!_examinaJahre.contains(jahr))
					{
						_examinaJahre.add(jahr);
					}

					String notiz = cursor.getString(ZNOTIZ);
					int fragenummer = (int) cursor.getInteger(ZFRAGENUMMER);
					int fragetag = (int) cursor.getInteger(ZFRAGETAG);

					Frage frage = new Frage(id, fragetext, bild, antworten, antwortbilder,
							richtigeantwort, fach, thema, jahr, notiz, fragenummer, fragetag);

					_fragen.add(frage);

				}
				while (cursor.next());
			}
		}
		finally
		{
			cursor.close();
		}
	}
}
